﻿using System;

namespace OS.Common.Core
{
    /// <summary>
    /// 自定义响应结果转换类
    /// </summary>
    public static class CustomResultMap
    {
        /// <summary>
        /// 将结果实体转换成其他结果实体
        /// </summary>
        /// <typeparam name="TParameter">输入参数</typeparam>
        /// <typeparam name="TResult">输出对象</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="func">转换方法</param>
        /// <returns></returns>
        public static CustomResult<TResult> ConvertToResult<TParameter, TResult>(this CustomResult<TParameter> source,
            Func<TParameter, TResult> func = null)
        {
            var result = new CustomResult<TResult>
            {
                Code = source.Code,
                Message = source.Message
            };
            if (func != null && source.Data != null)
                result.Data = func(source.Data);

            return result;
        }
    }
}